package com.jtmg.glorykingsnews.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jtmg.glorykingsnews.domain.TbBooth;
import com.jtmg.glorykingsnews.domain.TbManager;
import com.jtmg.glorykingsnews.domain.TbMarket;
import com.jtmg.glorykingsnews.mapper.TbManagerMapper;
import com.jtmg.glorykingsnews.mapper.TbMarketMapper;
import com.jtmg.glorykingsnews.vo.booth.AddBoothVo;
import com.jtmg.glorykingsnews.vo.booth.BoothVoForQuery;
import com.jtmg.glorykingsnews.vo.booth.QueryBoothVo;
import com.jtmg.glorykingsnews.vo.commom.LayuiReturnVo;
import com.jtmg.glorykingsnews.vo.commom.MsgVo;
import com.jtmg.glorykingsnews.vo.manager.ManagerVoForQuery;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.jtmg.glorykingsnews.mapper.TbBoothMapper;
import com.jtmg.glorykingsnews.service.TbBoothService;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class TbBoothServiceImpl implements TbBoothService{

    @Resource
    private TbBoothMapper tbBoothMapper;
    @Resource
    private TbManagerMapper tbManagerMapper;
    @Resource
    private TbMarketMapper tbMarketMapper;


    @Override
    public LayuiReturnVo<BoothVoForQuery> managerQueryBooth(QueryBoothVo queryBoothVo) {
       //不用判断deleted，因为该管理员可以登陆，那证明肯该管理员肯定没有被删除
        String managerId = queryBoothVo.getManagerId();
        TbManager manager = tbManagerMapper.selectByPrimaryKey(managerId);
        if (manager == null){
            return new LayuiReturnVo<BoothVoForQuery>(0,"查询摊位空值",0,null);
        }
        int marketId = manager.getMid();
        Example example = new Example(TbBooth.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("deleted",0)
                .andEqualTo("marketId",marketId);
        String sNumber = queryBoothVo.getNumber();
        String status = queryBoothVo.getStatus();
        System.out.println(status);
        if(sNumber!=null && !"".equals(sNumber)){
            int number = Integer.parseInt(sNumber);
            criteria.andEqualTo("number",number);
        }
        if(status!=null && !"".equals(status)){
            if(status!="0"){
                int sta = Integer.parseInt(status);
                criteria.andEqualTo("status",sta);
            }
        }
        int count = tbBoothMapper.selectCountByExample(example);
        List<TbBooth> boothList = tbBoothMapper.selectByExample(example);
        List<BoothVoForQuery> boothVoForQueries = new ArrayList<>();
        for (TbBooth booth : boothList){
            BoothVoForQuery boothVoForQuery = new BoothVoForQuery();
            boothVoForQuery.setId(booth.getId());
            boothVoForQuery.setMarketId(booth.getMarketId());
            //根据marketId查询marketName
            TbMarket market = tbMarketMapper.selectByPrimaryKey(booth.getMarketId());
            boothVoForQuery.setMarketName(market.getMarketName());
            boothVoForQuery.setSite(booth.getSite());
            if (booth.getSite() == 1){
                boothVoForQuery.setSiteValue("路东");
            }
            if (booth.getSite() == 2){
                boothVoForQuery.setSiteValue("路西");
            }
            if (booth.getSite() == 3){
                boothVoForQuery.setSiteValue("路南");
            }
            if (booth.getSite() == 4){
                boothVoForQuery.setSiteValue("路北");
            }
            boothVoForQuery.setNumber(booth.getNumber());
            boothVoForQuery.setSize(booth.getSize());
            boothVoForQuery.setPrice(booth.getPrice());
            boothVoForQuery.setStatus(String.valueOf(booth.getStatus()));
            if (booth.getStatus() == 1){
                boothVoForQuery.setStatusTs("已占用");
            }
            if (booth.getStatus() == 2){
                boothVoForQuery.setStatusTs("未占用");
            }
            if (booth.getStatus() == 3){
                boothVoForQuery.setStatusTs("暂不可用");
            }
            boothVoForQueries.add(boothVoForQuery);
        }

        return new LayuiReturnVo<BoothVoForQuery>(0,"查询摊位信息成功",count,boothVoForQueries);
    }

    @Override
    public PageInfo<TbBooth> userQueryBooth(QueryBoothVo queryBoothVo) {
        Example example = new Example(TbBooth.class);
        example.createCriteria().andEqualTo("deleted",0)
                .andEqualTo("marketId",queryBoothVo.getMarketId());
        String sNumber = queryBoothVo.getNumber();
        int number = Integer.parseInt(sNumber);
        String status = queryBoothVo.getStatus();
        if(sNumber!=null && !"".equals(sNumber)){
            example.createCriteria().andEqualTo("number",number);
        }
        if(status!=null && !"".equals(status)){
            example.createCriteria().andEqualTo("status",status);
        }
        PageHelper.startPage(queryBoothVo.getPage(), queryBoothVo.getLimit());
        return new PageInfo<>(tbBoothMapper.selectByExample(example));
    }

    @Override
    public MsgVo<TbBooth> queryBoothById(int id) {
        try {
            TbBooth booth = tbBoothMapper.selectByPrimaryKey(id);
            return new MsgVo<TbBooth>("200","查询摊位信息成功",booth);
        }catch (Exception e){
            return new MsgVo<TbBooth>("201","查询摊位信息异常");
        }
    }

    @Override
    public MsgVo addBooth(AddBoothVo vo) {

        System.out.println(vo.getManagerId()+"--"+vo.getNumber()+"--"+vo.getSite()+"--"+vo.getSize()+"--"+vo.getPrice());

        try {
            TbBooth booth = new TbBooth();
            //不用判断deleted，因为该管理员可以登陆，那证明肯该管理员肯定没有被删除
            int marketId = tbManagerMapper.selectByPrimaryKey(vo.getManagerId()).getMid();
            booth.setMarketId(marketId);
            booth.setSite(Integer.parseInt(vo.getSite())); //下拉框
            booth.setNumber(Integer.parseInt(vo.getNumber()));
            booth.setSize(Double.parseDouble(vo.getSize()));
            BigDecimal bPrice = new BigDecimal(vo.getPrice());
            booth.setPrice(bPrice);
            //默认状态是2 绿色 未占用
            booth.setStatus(2);
            //设置删除状态
            booth.setDeleted(0);
            tbBoothMapper.insert(booth);
            return new MsgVo("200","新增摊位成功");
        }catch (Exception e){
            return new MsgVo("201","新增摊位异常");
        }
    }

    @Override
    public MsgVo updateBooth(TbBooth tbBooth) {
        System.out.println(tbBooth.getNumber()+"--"+tbBooth.getStatus());
        try {
            Example example = new Example(TbBooth.class);
            example.createCriteria().andEqualTo("id",tbBooth.getId());
            tbBoothMapper.updateByExampleSelective(tbBooth,example);
            return new MsgVo("200","修改摊位信息成功");
        }catch (Exception e){
            return new MsgVo("201","修改摊位信息异常");
        }
    }

    @Override
    public MsgVo deleteBooth(int id) {
        try {
            Example example = new Example(TbBooth.class);
            example.createCriteria().andEqualTo("id",id);
            TbBooth booth =new TbBooth();
            booth.setDeleted(1);
            tbBoothMapper.updateByExampleSelective(booth,example);
            return new MsgVo("200","删除摊位信息成功");
        }catch (Exception e){
            return new MsgVo("201","删除摊位信息异常");
        }
    }
}
